package com;

import com.datasource.DynamicDataSourceRegister;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;

/**
 * @author linyukun
 * @date 2022/7/5 18:49
 */
//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
@Import({DynamicDataSourceRegister.class})
@MapperScan("com.dao")
@EnableSwagger2
public class Application extends SpringBootServletInitializer {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
       /* corsConfiguration.addAllowedOrigin("yzs.winshangdata.com");
        corsConfiguration.addAllowedOrigin("m.winshangdata.com");
        corsConfiguration.addAllowedOrigin("richweb.app.winshangdata.com ");*/

        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
    /**
     * 跨域过滤器
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }

    /**
     * 文件上传配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize("102400KB"); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("409600KB");
        return factory.createMultipartConfig();
    }

    //https://www.csdn.net/tags/NtjaMgwsODA3MzAtYmxvZwO0O0OO0O0O.html 访问本地图片
//    public ServletRegistrationBean getServletRegistrationBean() {
//        //放入自己的Servlet对象实例
//        ServletRegistrationBean bean = new ServletRegistrationBean(new GetPic());
//        //访问路径值
//        bean.addUrlMappings("/getPic");
//        //一定要返回ServletRegistrationBean
//        return bean;
//    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
